home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / yyref.shr / yyref / yyref-grammar < prev    next >
Encoding:
Text File  |  1993-07-04  |  4.5 KB  |  303 lines

  1. %{
  2.  
  3. # include <ctype.h>
  4.  
  5. int    line;
  6. %}
  7.  
  8.  
  9.     /*******************************************************\
  10.     *                            *
  11.     *    X_reference program for YACC files        *
  12.     *    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        *
  13.     *                            *
  14.     *    Cathy Taylor,                    *
  15.     *    c/o Department of Computing,            *
  16.     *    University of Lancaster,            *
  17.     *    Bailrigg, Lancaster, England.            *
  18.     *    Date : Fri Jul  4 00:50:04 BST 1986        *
  19.     *                            *
  20.     \*******************************************************/
  21.  
  22.  
  23.     /***********************************************\
  24.     *                        *
  25.     *    Yacc Input Syntax            *
  26.     *    ~~~~~~~~~~~~~~~~~            *
  27.     *                        *
  28.     *    Adapted from the document        *
  29.     *    'YACC - Yet Another Compiler Compiler'    *
  30.     *        by                *
  31.     *       S. C. Johnson            *
  32.     *                        *
  33.     *    Date: Tue Jul  1 02:40:18 BST 1986    *
  34.     *                        *
  35.     \***********************************************/
  36.  
  37.  
  38. %token    IDENTIFIER CHARACTER NUMBER
  39. %token    LEFT RIGHT NONASSOC TOKEN PREC TYPE START UNION
  40. %token    PER PERCURL ACT
  41. %token    COLON SEMICOLON COMMA OR LESS GREATER
  42.  
  43. %start    spec
  44.  
  45. %%
  46.  
  47. spec
  48.     :    defs PER rules tail
  49.             {
  50.                 printf("\n\n");
  51.                 yyclearin;
  52.                 return(0);
  53.             }
  54.     ;
  55.  
  56. tail
  57.     :    /* empty */
  58.     |    PER
  59.     |    error
  60.     ;
  61.  
  62. defs
  63.     :    /* empty */
  64.     |    def_bk
  65.     ;
  66.  
  67. def_bk
  68.     :    def
  69.     |    def_bk def
  70.     ;
  71.  
  72. def
  73.     :    START IDENTIFIER
  74.     |    UNION
  75.     |    PERCURL
  76.     |    rword tag nlist
  77.     |    tokendef
  78.     |    error
  79.     ;
  80.  
  81. rword
  82.     :    LEFT
  83.     |    RIGHT
  84.     |    NONASSOC
  85.     |    TYPE
  86.     ;
  87.  
  88. tokendef
  89.     :    TOKEN tokenlist
  90.     ;
  91.  
  92. tokenlist
  93.     :    IDENTIFIER
  94.         {
  95.             yyaction(ON_C_IDENT,line);
  96.         }
  97.     |    tokenlist opt_comma IDENTIFIER
  98.         {
  99.             yyaction(ON_C_IDENT,line);
  100.         }
  101.     ;
  102. tag
  103.     :    /* empty */
  104.     |    LESS IDENTIFIER GREATER
  105.     ;
  106.  
  107. nlist
  108.     :    nmno
  109.     |    nlist opt_comma nmno
  110.     ;
  111.  
  112. opt_comma
  113.     :    /* empty */
  114.     |    COMMA
  115.     ;
  116.  
  117. nmno
  118.     :    IDENTIFIER opt_num
  119.     ;
  120.  
  121. opt_num
  122.     :    /* empty */
  123.     |    NUMBER
  124.     ;
  125. rules
  126.     :    rule
  127.     |    rules rule
  128.     ;
  129.  
  130. rule
  131.     :    IDENTIFIER
  132.         {
  133.             yyaction(ON_C_IDENT,line);
  134.         }
  135.         COLON body SEMICOLON
  136.     |    error SEMICOLON
  137.     ;
  138.  
  139. body
  140.     :    body_block
  141.     |    body OR body_block
  142.     ;
  143.  
  144. body_block
  145.     :    /* empty */
  146.     |    body_block body_entity
  147.     ;
  148.  
  149. body_entity
  150.     :    opt_prec id_ent
  151.     |    ACT
  152.     ;
  153.  
  154. id_ent
  155.     :    IDENTIFIER
  156.         {
  157.             yyaction(ON_IDENT,line);
  158.         }
  159.     |    CHARACTER
  160.     ;
  161.  
  162. opt_prec
  163.     :    /* empty */
  164.     |    PREC
  165.     ;
  166.  
  167.  
  168. %%
  169.  
  170. # include    "lex.yy.c"
  171.  
  172. #define    ON_C_IDENT    000
  173. #define    ON_IDENT    001
  174.  
  175. #define    MAXIDENTS    1000
  176. #define    MAXCHARS    100
  177. #define    MAXDEFS        20
  178. #define    MAXOCCS        1000
  179.  
  180. struct    IREC {
  181.         char    ident[MAXCHARS];
  182.         int    desc[MAXDEFS];
  183.         int    nextdesc;
  184.         int    occ[MAXOCCS];
  185.         int    nextocc;
  186.         } table[MAXIDENTS];
  187.  
  188.  
  189. yyaction (action,ln)
  190. int    action;
  191. int    ln;
  192. {
  193.     int    id;
  194.     
  195.     id = 0;
  196.     while (    strcmp(table[id].ident,yytext) != 0 && strcmp(table[id].ident,"") != 0 )
  197.         id++;
  198.  
  199.     if ( strcmp(table[id].ident, yytext) != 0 )
  200.     {
  201.  
  202.     /*******************************************************\
  203.     *                            *
  204.     *    New non-terminal to be stored.            *
  205.     *    No distinction is made here between tokens    *
  206.     *    and (non) terminals.                *
  207.     *                            *
  208.     \*******************************************************/
  209.  
  210.         strcpy(table[id].ident,yytext);
  211.         table[id].nextdesc = 0;
  212.         table[id].nextocc = 0;
  213.     } /* fi */
  214.  
  215.     switch (action) {
  216.     case ON_C_IDENT:
  217.  
  218.     /*******************************************************\
  219.     *                            *
  220.     *    Add to list of definition lines.        *
  221.     *                            *
  222.     \*******************************************************/
  223.  
  224.         table[id].desc[table[id].nextdesc++] = ln;
  225.         break;
  226.  
  227.     case ON_IDENT:
  228.                 
  229.     /*******************************************************\
  230.     *                            *
  231.     *    Add to list of occurance lines.            *
  232.     *                            *
  233.     \*******************************************************/
  234.  
  235.         table[id].occ[table[id].nextocc++] = ln;
  236.         break;
  237.  
  238.     default        :
  239.         fprintf (stdout, "yyaction: invalid action\n");
  240.         return (-1);
  241.         } /* hctiws */
  242.     return (0);
  243. } /* corp */
  244.  
  245. nline(ln)
  246. int    ln;
  247. {
  248.     printf("%4d :\t",ln);
  249. }
  250.  
  251.  
  252.     char    declared_at_mark[] = "*";
  253.     char    occurs_at_mark[] = "";
  254.     char    token_maybe[] = "is not declared";
  255.     char    start_maybe[] = "never occurs on rhs of rule - start rule?";
  256.     
  257. /*
  258. *    Strings for output
  259. */
  260.  
  261. main ()
  262. {
  263.     int    ind,id;
  264.  
  265.     strcpy(table[0].ident,"");
  266.  
  267.     line = 0;
  268.     nline(++line);
  269.  
  270.     yyparse ();
  271.  
  272.     printf("\n\n~~~~~ Start of X-ref ~~~~~\n");
  273.     id = 0;
  274.     while( strcmp(table[id].ident,"") != 0 )
  275.     {
  276.         printf("\n%-20s ",table[id].ident);
  277.         if (table[id].nextdesc == 0 )
  278.             printf("    : %s",token_maybe);
  279.         else
  280.         {
  281.             ind = 0;
  282.             printf("%4d:",table[id].desc[ind++]);
  283.             for ( ind=1; ind < table[id].nextdesc ; ind++)
  284.             printf(" %s%4d",declared_at_mark,table[id].desc[ind]);
  285.         }
  286.         if (table[id].occ[0] == 0)
  287.             printf(" %s",start_maybe);
  288.         else
  289.         {
  290.             for ( ind = 0; ind < table[id].nextocc ; ind++ )
  291.             printf(" %s%4d",occurs_at_mark,table[id].occ[ind]);
  292.         }
  293.         id++;
  294.     }
  295.     printf("\n\n~~~~~ End of X-ref ~~~~~\n");
  296. } /* niam */
  297.  
  298. yyerror(mess)
  299. char    *mess;
  300. {
  301.     printf("\n\t%s\n",mess);
  302. } /* corp */
  303.